<template>
  <div>
    <van-nav-bar
      title="昵称"
      right-text="确定"
      left-arrow
      @click-left="$emit('close')"
      @click-right="editName"
    />
    <van-field
      v-model.trim="nc"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { editUserInfo } from '@/api/user.js'
export default {
  name: 'NicknamePup',
  props: {
    name: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      nc: this.name
    }
  },
  methods: {
    async editName () {
      if (this.nc === '') {
        return
      }
      if (this.nc === this.name) {
        return this.$emit('close')
      }
      try {
        await editUserInfo({
          name: this.nc
        })
        this.$emit('editName', this.nc)
        this.$toast('修改名字成功')
      } catch (err) {
        this.$toast('修改名字失败')
      }
    }
  }
}
</script>

<style></style>
